package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.dao.*;
import com.stylefeng.guns.common.persistence.model.MonthbalanceDetail;
import com.stylefeng.guns.common.persistence.model.TBill;
import com.stylefeng.guns.common.persistence.model.TOrder;
import com.stylefeng.guns.common.persistence.model.TOrderFee;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.service.ITOrderFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  订单费用
 * </p>
 *
 * @author xiejie123
 * @since 2018-01-28
 */
@Service
public class TOrderFeeServiceImpl extends ServiceImpl<TOrderFeeMapper, TOrderFee> implements ITOrderFeeService {

    @Autowired
    TOrderFeeMapper tOrderFeeMapper;
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    TBillMapper billMapper;

    @Autowired
    TTrainMapper tTrainMapper;

    @Autowired
    MonthbalanceDetailMapper monthbalanceDetailMapper;

    @Autowired
    private DictMapper tDictMapper;

    @Override
    public List<Map<String, Object>> selectOrderFeeList(Pagination page, Map param) {
        return tOrderFeeMapper.selectOrderFeeList(page,param);
    }

    @Override
    public List<Map<String, Object>> selectOrderFeeOffList(Pagination page, Map param) {
        return  tOrderFeeMapper.selectOrderFeeOffList(page,param);
    }

    @Override
    public List<Map<String, Object>> selectFeeList(List<Integer> ids) {
        return tOrderFeeMapper.selectFeeList(ids);
    }

    @Override
    public List<Map<String, Object>> getFee(String deptid) {
        return tOrderFeeMapper.getFee(deptid);
    }

    @Override
    public List<Map<String, Object>> selectOrderFeeOffSum(Map param) {
        return tOrderFeeMapper.selectOrderFeeOffSum(param);
    }

    @Override
    public List<Map<String, Object>> selectOrderFeeLSum(Map param) {
        return tOrderFeeMapper.selectOrderFeeLSum(param);
    }

    @Override
    public int updateOrderFee(String pay_mode, String order_id, String use_object) {
        return tOrderFeeMapper.updateOrderFee(pay_mode,order_id,use_object);
    }

    @Override
    @Transactional
    public int updateOrderFee(String order_id, String use_object, TBill bill,TOrder order,String xj) {

        TOrder t =tOrderMapper.selectById(order.getId());
        tOrderMapper.updateById(order);

        billMapper.insert(bill);

        Map param1=new HashMap();
        param1.put("dot_id",ShiroKit.getUser().getDotId());
        param1.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
//        String bath_no = tDictMapper.getNumCode(param1);

        MonthbalanceDetail m=new MonthbalanceDetail();
        m.setCreater(ShiroKit.getUser().getId());
        m.setDotId(ShiroKit.getUser().getDotId());
        m.setIncome(2);//来源 1，收货，2，自提，3，配送，4，中转，5，改单
        m.setStatus("1");//0待结算 1 已结算
        m.setSettlement_type(0);//付款方式，0现结 1月结
        m.setMoney(Double.parseDouble(xj));
        m.setTraner(t.getConsignee());
        m.setIncomeId(t.getId());
        m.setIncomeName(t.getOriginal_num());
        m.setBill_id(bill.getId());
//        m.setBalance_num(bath_no);
        m.setBalance_num(tDictMapper.getNumCode(param1));

        monthbalanceDetailMapper.insert(m);


        EntityWrapper<TOrderFee> eWrapper = new EntityWrapper<TOrderFee>();
        eWrapper.where("order_id={0}",order.getId());
        List<Map<String,Object>> updateFeeids=tOrderFeeMapper.selectUpFeeid(order_id,"3");

        for(Map map:updateFeeids){
//            Map param =new HashMap();
//            param.put("order_fee_id",map.get("id").toString());
//            param.put("order_id",map.get("order_id").toString());
//            param.put("bill_id",bill.getId());
//            tTrainMapper.addFeeBill(param);

            TOrderFee tOrderFee=new TOrderFee();
            tOrderFee.setAuditStatus(1);//0 未结算 1 已结算 2 坏账 3 清账
            tOrderFee.setSettlementType("0");
            tOrderFee.setBalance_id(m.getId());
            tOrderFee.setId(Integer.valueOf(map.get("id").toString()));
            tOrderFeeMapper.updateById(tOrderFee);
        }
        return 0;
    }

    @Override
    @Transactional
    public void updateSelfLiftFees(List<TOrder> list, TBill bill, String xj) {
        billMapper.insert(bill);

        Map para=new HashMap();
        para.put("dot_id",ShiroKit.getUser().getDotId());
        para.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结

        MonthbalanceDetail m=new MonthbalanceDetail();
        m.setCreater(ShiroKit.getUser().getId());
        m.setDotId(ShiroKit.getUser().getDotId());
        m.setIncome(2);//来源 1，收货，2，自提，3，配送，4，中转，5，改单
        m.setStatus("1");//0待结算 1 已结算
        m.setSettlement_type(0);//付款方式，0现结 1月结
        m.setMoney(Double.parseDouble(xj));
//        m.setTraner(t.getConsignee());
//        m.setIncomeId(t.getId());
//        m.setIncomeName(t.getOriginal_num());
        m.setBill_id(bill.getId());
        m.setBalance_num(tDictMapper.getNumCode(para));
        monthbalanceDetailMapper.insert(m);

        for (TOrder torder:list){
            tOrderMapper.updateById(torder);
            EntityWrapper<TOrderFee> eWrapper = new EntityWrapper<TOrderFee>();
            eWrapper.where("order_id={0}",torder.getId());
            List<Map<String,Object>> updateFeeids=tOrderFeeMapper.selectUpFeeid(torder.getId()+"","3");
            for(Map map:updateFeeids){
                TOrderFee tOrderFee=new TOrderFee();
                tOrderFee.setAuditStatus(1);//0 未结算 1 已结算 2 坏账 3 清账
                tOrderFee.setSettlementType("0");
                tOrderFee.setBalance_id(m.getId());
                tOrderFee.setId(Integer.valueOf(map.get("id").toString()));
                tOrderFeeMapper.updateById(tOrderFee);
            }
        }
    }

}
